import os
import re

import xlrd
class EnAndValue:
    def __init__(self, en, value = None, key = None):
        self.en = en
        self.value = value
        self.key = key
class Excel2XmlWithoutKey:
    keyColumnIndex = 0
    enColumnIndex = 1
    valueColumnIndex = 2
    startRowIndex = 1

    def __init__(self, excelFileAbsPath, sheetName, keyFileAbsPath, excel2XmlFileAbsPath):
        """
        :param excelFileAbsPath: 待迁移的 excel 文件
        :param keyFileAbsPath: 英语 strings.xml 文件
        """
        self.excelFileAbsPath = excelFileAbsPath
        self.sheetName = sheetName
        self.keyFileAbsPath = keyFileAbsPath
        self.excel2XmlFileAbsPath = excel2XmlFileAbsPath
    def excel2List(self):
        book = xlrd.open_workbook(self.excelFileAbsPath)
        sheet = book.sheet_by_name(self.sheetName)
        result = []
        for rx in range(sheet.nrows):
            if rx < self.startRowIndex:
                continue
            row = sheet.row(rx)
            item = EnAndValue(row[self.enColumnIndex].value, row[self.valueColumnIndex].value, row[self.keyColumnIndex].value)
            result.append(item)
        return result

    def keyFile2List(self):
        file = open(self.keyFileAbsPath, 'r')
        fileContent = file.read()
        searchIndex = 0
        keyValueRegex = re.compile(r'<string\s+name\s*=\s*"([^"]+)"\s*>([\s\S]+?)</\s*string\s*>')

        keyValueRegexResult = keyValueRegex.search(fileContent, searchIndex)
        result = []
        while keyValueRegexResult:
            result.append(EnAndValue(keyValueRegexResult.group(2), None, keyValueRegexResult.group(1)))
            searchIndex = keyValueRegexResult.span()[1]
            keyValueRegexResult = keyValueRegex.search(fileContent, searchIndex)
        return result

    def compare(self, excelElementList, keyFileElementList):
        foundList = []
        notFoundList = []
        for excelElement in excelElementList:
            found = False
            for keyFileElement in keyFileElementList:
                if keyFileElement.key == excelElement.key:
                    foundList.append(excelElement)
                    found = True
                    break
            if not found:
                notFoundList.append(excelElement)
        if not os.path.exists(self.excel2XmlFileAbsPath[0 : self.excel2XmlFileAbsPath.rindex(os.sep)]):
            os.makedirs(self.excel2XmlFileAbsPath[0 : self.excel2XmlFileAbsPath.rindex(os.sep)])
        #if not os.path.exists(self.excel2XmlFileAbsPath):
        file = open(self.excel2XmlFileAbsPath, 'w')
        for element in foundList:
            file.write('<string name="{0}">{1}</string>\n'.format(element.key, element.value))

        print("-----------------------------[开始]-----------------------------")
        for element in notFoundList:
            print("en: {0}, value: {1}".format(element.en, element.value))
        print("-----------------------------[结束]-----------------------------")
    def doTask(self):
        excelElementList = self.excel2List()
        keyFileElementList = self.keyFile2List()
        self.compare(excelElementList, keyFileElementList)
if __name__ == '__main__':
    task1 = Excel2XmlWithoutKey('/Users/h/Downloads/希腊语 2/Android 希腊语缺失文案.xls',
                                "待翻译",
                                "/Users/h/AndroidStudioProjects/patpat/Patpat/src/main/res/values/strings.xml",
                                "/Users/h/Desktop/AndroidStringFiles/values-el/strings.xml")
    task2 = Excel2XmlWithoutKey("/Users/h/Downloads/土耳其/Android 土耳其语缺失文案.xls",
                                "待翻译",
                                "/Users/h/AndroidStudioProjects/patpat/Patpat/src/main/res/values/strings.xml",
                                "/Users/h/Desktop/AndroidStringFiles/values-tr/strings.xml")
    task1.doTask()
    task2.doTask()
